<?php

namespace app\index\controller;

use app\index\model\FlowBillModel;
use app\index\model\FlowClassModel;
use app\index\model\FlowCourseModel;
use app\index\model\FlowPromerModel;
use app\index\model\FlowSetModel;
use app\index\model\FlowTaskModel;
use app\index\model\FlowTaskShenModel;
use app\index\model\HuiModel;
use app\index\model\MeetModel;
use app\index\model\NoticeModel;
use app\index\model\TodoModel;
use app\index\model\UserModel;
use app\index\service\FlowService;
use app\index\service\OptService;
use app\index\service\XingService;
use think\Db;
use think\Exception;
use think\Request;

class Welcome extends Common
{
    public function get_shen(){
        $data['avatar'] = $this->userinfo['avatar'];
        $data['realname'] = $this->userinfo['realname'];
        $data['lastoptdt'] = $this->userinfo['lastoptdt'];

        $data['bill_create'] = FlowBillModel::alias('bill')
            ->join('user user','user.id=bill.created_user')
            ->where(['bill.deleted_user'=>0,'bill.comid'=>$this->userinfo['comid']])
            ->where(['bill.isturn'=>1,'bill.created_user'=>$this->userinfo['id']])
            ->count();

        $data['bill_hand'] = FlowBillModel::alias('bill')
            ->join('user user','user.id=bill.created_user')
            ->where(['bill.deleted_user'=>0,'bill.comid'=>$this->userinfo['comid']])
            ->where(['bill.isturn'=>1,'bill.status'=>0,'bill.created_user'=>$this->userinfo['id']])
            ->count();

        $data['bill_finish'] = FlowBillModel::alias('bill')
            ->join('user user','user.id=bill.created_user')
            ->where(['bill.deleted_user'=>0,'bill.comid'=>$this->userinfo['comid']])
            ->where(['bill.isturn'=>1,'bill.created_user'=>$this->userinfo['id']])
            ->where('bill.status','in', [1,2,3,5])
            ->count();

        $data['bill_turn'] = FlowBillModel::alias('bill')
            ->join('user user','user.id=bill.created_user')
            ->where(['bill.deleted_user'=>0,'bill.comid'=>$this->userinfo['comid']])
            ->where(['bill.isturn'=>0,'bill.created_user'=>$this->userinfo['id']])
            ->count();


        $data['flow_hand'] = FlowTaskModel::alias('task')
            ->join('flow_bill bill','bill.id=task.billid')
            ->leftJoin('user user','user.id=bill.created_user')
            ->where(['bill.deleted_user'=>0,'bill.comid'=>$this->userinfo['comid']])
            ->where(['bill.isturn'=>1,'bill.status'=>0])
            ->where(['task.deleted_user'=>0,'task.uid'=>$this->userinfo['id'],'task.state'=>1])
            ->group('bill.id')
            ->count();

        $data['flow_finish'] = FlowTaskShenModel::alias('shen')
            ->join('flow_bill bill','bill.id=shen.billid')
            ->leftJoin('user user','user.id=bill.created_user')
            ->where(['bill.deleted_user'=>0,'bill.comid'=>$this->userinfo['comid']])
            ->where(['bill.isturn'=>1])
            ->where(['shen.uid'=>$this->userinfo['id']])
            ->group('bill.id')
            ->count();

        $data['flow_promer'] = FlowPromerModel::alias('promer')
            ->join('flow_bill bill','promer.billid = bill.id')
            ->join('user user','user.id=bill.created_user')
            ->join('user fen','fen.id=promer.uid')
            ->where(['bill.deleted_user'=>0,'bill.comid'=>$this->userinfo['comid']])
            ->where(['bill.isturn'=>1])
            ->count();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_notice(){
        $data['notice'] = NoticeModel::where(['comid'=>$this->userinfo['comid']])
            ->where(['deleted_user'=>0])
            ->where(['status'=>1])
            ->where(Db::raw("find_in_set(".$this->userinfo['id'].",jieuser) or jieuser='' or jieuser is null"))
            ->order('ding DESC,id DESC')
            ->limit(10)
            ->select()->toArray();
        foreach ($data['notice'] as &$item){
            $item['tongtype'] = (new XingService())->get_notice_tongtype_show($item['tongtype']);
            $item['created_at'] = date("Y-m-d H:i", strtotime($item['created_at']));
            $item['created_ymd'] = date("Y-m-d", strtotime($item['created_at']));
        }

        $data['hui'] = HuiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->field('id,title,created_user,reader,created_at')
            ->where(Db::raw("FIND_IN_SET(".$this->userinfo['id'].",`porter`)"))
            ->order('id DESC')
            ->limit(5)
            ->select()->toArray();
        foreach ($data['hui'] as &$item){
            $item['created_at'] = date("Y-m-d H:i", strtotime($item['created_at']));
            $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);

            $item['read'] = '未阅';
            if(in_array($this->userinfo['id'], explode(',', $item['reader']))){
                $item['read'] = '已阅';
            }
        }


        $data['meet'] = MeetModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(Db::raw("find_in_set(".$this->userinfo['id'].",caner) or created_user=".$this->userinfo['id']))
            ->where('startdt','between',[date("Y-m-d 00:00:00"), date("Y-m-d 23:59:59")])
            ->order('startdt ASC')
            ->limit(5)
            ->select()->toArray();
        foreach ($data['meet'] as &$item){
            $item['room'] = (new OptService())->get_base_showname('qi_meet_room', $item['room']);
            $item['status_name'] = (new XingService())->get_meet_status($item['startdt'], $item['enddt']);
            $item['startdt'] = date("m-d H:i", strtotime($item['startdt']));
        }

        $data['yujing'] = TodoModel::where(['uid'=>$this->userinfo['id'],'xiaolei'=>2,'status'=>0])
            ->field('id,mess,mid,table')
            ->order('id DESC')
            ->limit(5)
            ->select()->toArray();


        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_jinflow(){
        $data = [];

        $where = [];
        //排除不能直接添加的流程，比如聘费补充合同
        $where[] = ['modeid','neq',46];

        $jin = FlowBillModel::field('modenum,modename')->where(['deleted_user'=>0,'created_user'=>$this->userinfo['id']])
            ->group('modeid')->where($where)->order(Db::raw("MAX(created_at) DESC"))->limit(5)->select()->toArray();
        foreach ($jin as $v){
            $data[] = $v;
        }

        $data[] = ['modenum'=>'zhi_daybao','modename'=>'日常费用报销'];
        $data[] = ['modenum'=>'zhi_dayfu','modename'=>'日常付款'];
        $data[] = ['modenum'=>'seal_gai','modename'=>'日常盖章申请'];
        $data[] = ['modenum'=>'wu_ling','modename'=>'物品领用'];
        $data[] = ['modenum'=>'tongyong','modename'=>'通用事项审批'];

        foreach ($data as $key=>&$item){
            $set = FlowSetModel::where(['modenum'=>$item['modenum']])->find();
            $exist = FlowClassModel::where(['comid'=>$this->userinfo['comid'],'setid'=>$set['id']])->find();
            $item['status'] = $exist['status'];
            $item['ispublish'] = false;
            if(FlowCourseModel::where(['comid'=>$this->userinfo['comid'],'classid'=>$exist['id'],'status'=>1,'deleted_user'=>0])->find()){
                $item['ispublish'] = true;
            }
        }

        return outputJson(2000,lang('code_2000'),$data);
    }



    public function get_shen_mob(){
        $data['dainum'] = FlowTaskModel::alias('task')
            ->join('flow_bill bill','bill.id=task.billid')
            ->where(['bill.comid'=>$this->userinfo['comid']])
            ->where(['bill.deleted_user'=>0])
            ->where(['bill.isturn'=>1])
            ->where(['bill.status'=>0])
            ->where(['task.uid'=>$this->userinfo['id']])
            ->where(['task.state'=>1])
            ->count();
        $data['dai'] = FlowTaskModel::alias('task')
            ->field('bill.*')
            ->join('flow_bill bill','bill.id=task.billid')
            ->where(['bill.comid'=>$this->userinfo['comid']])
            ->where(['bill.deleted_user'=>0])
            ->where(['bill.isturn'=>1])
            ->where(['bill.status'=>0])
            ->where(['task.uid'=>$this->userinfo['id']])
            ->where(['task.state'=>1])
            ->order('bill.id DESC')
            ->limit(3)
            ->select()->toArray();
        foreach ($data['dai'] as &$item){
            $item['status_name'] = (new FlowService())->get_flow_status_name($item);
        }


        $data['finishnum'] = FlowTaskShenModel::alias('shen')
            ->field('bill.*')
            ->join('flow_bill bill','bill.id=shen.billid')
            ->join('user user','user.id=bill.created_user')
            ->where(['bill.comid'=>$this->userinfo['comid']])
            ->where(['bill.deleted_user'=>0])
            ->where(['bill.isturn'=>1])
            ->where(['shen.uid'=>$this->userinfo['id']])
            ->group('bill.id')
            ->count();
        $data['finish'] = FlowTaskShenModel::alias('shen')
            ->join('flow_bill bill','bill.id=shen.billid')
            ->join('user user','user.id=bill.created_user')
            ->where(['bill.comid'=>$this->userinfo['comid']])
            ->where(['bill.deleted_user'=>0])
            ->where(['bill.isturn'=>1])
            ->where(['shen.uid'=>$this->userinfo['id']])
            ->group('bill.id')
            ->order('bill.id DESC')
            ->limit(3)
            ->select()->toArray();
        foreach ($data['finish'] as &$item){
            $item['status_name'] = (new FlowService())->get_flow_status_name($item);
        }

        $data['chaonum'] = FlowPromerModel::alias('promer')
            ->join('flow_bill bill','promer.billid = bill.id')
            ->join('user user','user.id=bill.created_user')
            ->join('user fen','fen.id=promer.uid')
            ->where(['bill.comid'=>$this->userinfo['comid']])
            ->where(['bill.deleted_user'=>0])
            ->where(['bill.created_user'=>$this->userinfo['id']])
            ->where(['bill.isturn'=>1])
            ->where(Db::raw("find_in_set(".$this->userinfo['id'].",domer)"))
            ->count();
        $data['chao'] = FlowPromerModel::alias('promer')
            ->field('bill.*')
            ->join('flow_bill bill','promer.billid = bill.id')
            ->join('user user','user.id=bill.created_user')
            ->join('user fen','fen.id=promer.uid')
            ->where(['bill.comid'=>$this->userinfo['comid']])
            ->where(['bill.deleted_user'=>0])
            ->where(['bill.created_user'=>$this->userinfo['id']])
            ->where(['bill.isturn'=>1])
            ->where(Db::raw("find_in_set(".$this->userinfo['id'].",domer)"))
            ->order('bill.id DESC')
            ->limit(3)
            ->select()->toArray();
        foreach ($data['chao'] as &$item){
            $item['status_name'] = (new FlowService())->get_flow_status_name($item);
        }



        $data['bill_hand'] = FlowBillModel::alias('bill')
            ->join('user user','user.id=bill.created_user')
            ->where(['bill.deleted_user'=>0,'bill.comid'=>$this->userinfo['comid']])
            ->where(['bill.isturn'=>1,'bill.status'=>0,'bill.created_user'=>$this->userinfo['id']])
            ->count();

        $data['bill_finish'] = FlowBillModel::alias('bill')
            ->join('user user','user.id=bill.created_user')
            ->where(['bill.deleted_user'=>0,'bill.comid'=>$this->userinfo['comid']])
            ->where(['bill.isturn'=>1,'bill.created_user'=>$this->userinfo['id']])
            ->where('bill.status','in', [1,2,3,5])
            ->count();

        $data['bill_turn'] = FlowBillModel::alias('bill')
            ->join('user user','user.id=bill.created_user')
            ->where(['bill.deleted_user'=>0,'bill.comid'=>$this->userinfo['comid']])
            ->where(['bill.isturn'=>0,'bill.created_user'=>$this->userinfo['id']])
            ->count();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_jinflow_mob(){
        $data = [];
        $where = [];
        //排除不能直接添加的流程，比如聘费补充合同
        $where[] = ['modeid','neq',46];

        $jin = FlowBillModel::field('modenum,modename')->where(['deleted_user'=>0,'created_user'=>$this->userinfo['id']])
            ->group('modeid')->where($where)->order(Db::raw("MAX(created_at) DESC"))->limit(2)->select()->toArray();
        foreach ($jin as $v){
            $data[] = $v;
        }

        $data[] = ['modenum'=>'zhi_daybao','modename'=>'日常费用报销'];
        $data[] = ['modenum'=>'zhi_dayfu','modename'=>'日常付款'];
        $data[] = ['modenum'=>'seal_gai','modename'=>'日常盖章申请'];
        $data[] = ['modenum'=>'wu_ling','modename'=>'物品领用'];
        $data[] = ['modenum'=>'tongyong','modename'=>'通用事项审批'];

        foreach ($data as $key=>&$item){
            $set = FlowSetModel::where(['modenum'=>$item['modenum']])->find();
            $exist = FlowClassModel::where(['comid'=>$this->userinfo['comid'],'setid'=>$set['id']])->find();
            $item['status'] = $exist['status'];
            $item['ispublish'] = false;
            if(FlowCourseModel::where(['comid'=>$this->userinfo['comid'],'classid'=>$exist['id'],'status'=>1,'deleted_user'=>0])->find()){
                $item['ispublish'] = true;
            }
        }

        return outputJson(2000,lang('code_2000'),$data);
    }



}